<?php


namespace queue;

use think\queue\Job;

/**
 * 消息队列基类
 * Class BaseJobs
 * @package queue
 */
abstract class BaseJobs
{
    /**
     * @param $name
     * @param $arguments
     */
    public function __call($name, $arguments)
    {
        $this->fire(...$arguments);
    }

    /**
     * 运行消息队列
     * @param Job $job
     * @param $data
     */
    public function fire(Job $job, $data): void
    {
        try {
            $action     = $data['do'] ?? 'doJob';//任务名
            $infoData   = $data['data'] ?? [];//执行数据
            $errorCount = $data['errorCount'] ?? 0;//最大错误次数

            $this->runJob($action, $job, $infoData, $errorCount);
        } catch (\Throwable $e) {
            $job->delete();
        }
    }

    /**
     * 执行队列
     * @param string $action
     * @param Job $job
     * @param array $infoData
     * @param int $errorCount
     */
    protected function runJob(string $action, Job $job, array $infoData, int $errorCount = 3)
    {

        $action = method_exists($this, $action) ? $action : 'handle';
        if (!method_exists($this, $action)) {
            $job->delete();
        }
        $datav['msg']      = 'BaseJobs/runJob开始执行-';
        $datav['action']   = $action;
        $datav['infoData'] = $infoData;
        $datav['this']     = $this;
        rwf('job0322.txt', $datav);
        if ($this->{$action}($infoData)) {
            //删除任务
            $job->delete();
        } else {
            if ($job->attempts() >= $errorCount && $errorCount) {
                //删除任务
                $job->delete();
            } else {
                //从新放入队列
                $job->release();
            }
        }
    }

    /**
     * 发布失败
     * @param $data
     */
    public function failed($data)
    {
        // todo ...任务达到最大重试次数后，失败了
        $datav['msg']  = '任务达到最大重试次数后，失败了';
        $datav['data'] = $data;
        rwf('job0322.txt', $datav);
        return true;
    }
}